package com.cyzy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cyzy.mapper.MenuMapper;
import com.cyzy.pojo.AdminUser;
import com.cyzy.pojo.Menu;
import com.cyzy.pojo.RoleMenu;
import com.cyzy.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {

    @Autowired
    private MenuMapper menuMapper;


    @Override
    public List<Menu> selectMenuList(String username) {
        return menuMapper.selectMenuList(username);
    }

    @Override
    public List<RoleMenu> selectRoleMenuList() {
        return menuMapper.selectRoleMenuList();
    }

    @Override
    public List<RoleMenu> searchworkersAll(String username, String role, Integer state) {
        return menuMapper.searchworkersAll(username,role,state);
    }

    @Override
    public List<Menu> selectAllMenuList() {
        QueryWrapper<Menu> queryWrapper = new QueryWrapper<>();
        queryWrapper.ne("menu_state", 2);
        return this.list(queryWrapper);
    }

    @Override
    public List<Menu> searchMenuAll(String menuName, Integer menuState) {
        return menuMapper.searchMenuAll(menuName,menuState);
    }

    @Override
    public List<Menu> getMenuAllList() {
        QueryWrapper<Menu> queryWrapper = new QueryWrapper<>();
        return menuMapper.selectList(queryWrapper);
    }

    @Override
    public Menu selectMenu(String menuName) {
        QueryWrapper<Menu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("menu_name",menuName);
        return getOne(queryWrapper);
    }

    @Override
    public Menu selectMenuById(Integer menuId) {
        QueryWrapper<Menu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("menu_id",menuId);
        return getOne(queryWrapper);
    }

    @Override
    public int updateManage(Integer menuId, String menuName, String path, Integer menuState, String name, String component, String icon) {
        LambdaUpdateWrapper<Menu> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(Menu::getMenuId,menuId);
        wrapper.set(Menu::getMenuName,menuName);
        wrapper.set(Menu::getPath,path);
        wrapper.set(Menu::getMenuState,menuState);
        wrapper.set(Menu::getName,name);
        wrapper.set(Menu::getComponent,component);
        wrapper.set(Menu::getIcon,icon);
        return menuMapper.update(null, wrapper);
    }

    @Override
    public int deleteState(Integer menuId, Integer menuState) {
        LambdaUpdateWrapper<Menu> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(Menu::getMenuId,menuId);
        wrapper.set(Menu::getMenuState,2);
        return menuMapper.update(null, wrapper);
    }

    @Override
    public int deleteStateByIds(List<String> ids, String state) {
        UpdateWrapper<Menu> updateWrapper = new UpdateWrapper<>();
        updateWrapper.in("menu_id", ids); // 条件：ID 在传入的列表中

        Menu menu = new Menu();
        menu.setMenuState(2); // 状态设为 2 表示删除（逻辑删除）

        return menuMapper.update(menu, updateWrapper); // 返回受影响行数
    }
}
